package com.zjh.rocketmq.listener;

import com.zjh.rocketmq.annotation.RocketmqTransactionListener;
import org.apache.rocketmq.spring.core.RocketMQLocalTransactionListener;
import org.apache.rocketmq.spring.core.RocketMQLocalTransactionState;
import org.springframework.messaging.Message;
import org.springframework.stereotype.Service;

@Service
@RocketmqTransactionListener(topic = "topic1", selectorExpression = "11111")
public class OrderListener implements RocketMQLocalTransactionListener {
    @Override
    public RocketMQLocalTransactionState executeLocalTransaction(Message msg, Object arg) {
        try {
            System.out.println("order执行的操作");
            //todo 数据库相关逻辑  报错事务回滚 否则事务提交
            int count = 0;
            System.err.println("==================================开始执行事务order===============================");
            return count > 0 ? RocketMQLocalTransactionState.COMMIT : RocketMQLocalTransactionState.ROLLBACK;
        } catch (Exception e) {
            System.err.println("=====================================事务执行失败order============================================");
            return RocketMQLocalTransactionState.ROLLBACK;
        }
    }

    @Override
    public RocketMQLocalTransactionState checkLocalTransaction(Message msg) {
        try {
            //todo 数据库相关逻辑  报错事务回滚 否则事务提交
            boolean flag = true;
            return flag ? RocketMQLocalTransactionState.COMMIT : RocketMQLocalTransactionState.ROLLBACK;
        } catch (Exception e) {
            System.err.println("=====================================事务执行失败order============================================");
            return RocketMQLocalTransactionState.ROLLBACK;
        }
    }
}
